﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Acms.Bll;
using System.Text;
/// <summary>
/// Summary description for BaseTopicPage
/// </summary>
public abstract class BaseTopicPage : BasePage
{
    protected override void OnLoad(EventArgs e)
    {
        if (!IsPostBack)
        {
            if(!string.IsNullOrEmpty(Request.QueryString["Id"]))
            {
                try
                {
                    TopicId = int.Parse(Request.QueryString["Id"]);
                }
                catch{
                }
            }
            if(!string.IsNullOrEmpty(Request.QueryString["GroupId"]))
            {
                try
                {
                    TopicGroupId = int.Parse(Request.QueryString["GroupId"]);
                }
                catch{
                }
            }
        }
        base.OnLoad(e);
    }

    public int TopicId { get; private set; }

    public int TopicGroupId { get; private set; }

    protected override object SaveViewState()
    {
        ViewState["TopicId"] = TopicId;
        ViewState["TopicGroupId"] = TopicGroupId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if (ViewState["TopicId"] is int)
        {
            TopicId = (int)ViewState["TopicId"];
        }
        if (ViewState["TopicGroupId"] is int)
        {
            TopicGroupId = (int)ViewState["TopicGroupId"];
        }
    }

    private Topic currentTopic;
    public Topic CurrentTopic
    {
        get 
        {
            if (currentTopic == null && TopicId > 0)
            {
                currentTopic = Topic.GetById(TopicId);
            }
            return currentTopic;
        }
    }

    private TopicGroup currentGroup;
    public TopicGroup CurrentGroup
    {
        get
        {
            if (currentGroup == null && TopicGroupId > 0)
            {
                currentGroup = TopicGroup.GetById(TopicGroupId);
            }
            return currentGroup;
        }
    }

    public string AppendQueryStringParameters(string url)
    {
        StringBuilder tmp = new StringBuilder(url);
        string spacer = "&";
        if(!url.Contains("?"))
        {
            spacer = "?";
        }
        if (TopicId > 0)
        {
            tmp.AppendFormat("{0}Id={1}", spacer, TopicId);
            spacer = "&";
        }
        if (TopicGroupId > 0)
        {
            tmp.AppendFormat("{0}GroupId={1}", spacer, TopicGroupId);
        }
        return tmp.ToString();
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        breadcrumbs.Add("Settings", null);
        breadcrumbs.Add("Topics", "~/Topics");
    }
}
